<template>
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('homepage.WarehouseManage.positionCode')" />
		<!-- 表格数据 -->
		<zy-table :headList="headList" :dataList="dataList" @editInput="editInput" border>
		</zy-table>
		<!-- 底部按钮 -->
		<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('public.submit')" @rightBtn="submitBtn"
			@leftBtn="resetBtn" :leftDisabled="disabled" :rightDisabled="disabled">
		</bottomBtn>
		<!-- 盘点提交二次确认 -->
		<zy-popup ref="popup" :title="$t('homepage.WarehouseManage.inventorySure')" type="dialog" @confirm="confirm">
			{{$t('homepage.WarehouseManage.confirmInventorySure')}}
		</zy-popup>
	</view>
</template>
<script>
	import {
		queryListByPositionCode,
		saveStorehouseInventory
	} from "@/api/homepage.js"
	import search from "@/components/topSearch/index.vue";
	import bottomBtn from "@/components/bottomBtn/index.vue";
	export default {
		components: {
			search,
			bottomBtn
		},
		data() {
			return {
				//表格表头
				headList: [{
					name: this.$t('homepage.WarehouseManage.instorageCode'),
					key: 'barcode',
				}, {
					name: this.$t('public.surplusNum'),
					key: 'surplusNum',
				}, {
					name: this.$t('homepage.WarehouseManage.inventoryNum'),
					key: 'num',
					edit: true,
					type: 'number'
				}],
				dataList: [], //表格数据
				disabled: true,
				dataArray: []
			}
		},
		onLoad() {
			//PDA扫描
			var _this = this
			uni.$on('scancodedate', function(data) {
				_this.searchList(data.code)
			})
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
		},
		methods: {
			searchList(e) {
				this.dataList = []
				queryListByPositionCode({
					warehousePositionCode: e,
				}).then((res) => {
					if (res.code == 0) {
						this.dataList = res.data
						this.disabled = false
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//编辑出库数量
			editInput(val) {
				if (Number(val.item.num) > Number(this.dataList[val.index].surplusNum)) {
					this.dataList[val.index].num = this.dataList[val.index].surplusNum
					uni.showToast({
						title: this.$t('public.max') + this.$t('public.colon') + this.dataList[val.index].surplusNum,
						icon: "none",
						duration: 3000
					})
				}
			},
			//重置按钮
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/WarehouseManage/StorageManagement/Inventory/index"
				})
			},
			//提交按钮
			submitBtn() {
				this.$refs.popup.open()
			},
			//确认提交件
			confirm() {
				this.dataArray = []
				this.$refs.popup.close()
				for (var i = 0; i < this.dataList.length; i++) {
					let obj = {}
					obj.num = this.dataList[i].num
					obj.storeNum = this.dataList[i].surplusNum
					obj.materialId = this.dataList[i].materialId
					obj.warehouseStorageId = this.dataList[i].warehouseStorageId
					obj.warehousePositionId = this.dataList[i].warehousePositionId
					obj.foreignId = this.dataList[i].foreignId
					obj.storehouseDetailId = this.dataList[i].id
					this.dataArray.push(obj)
				}
				saveStorehouseInventory({
					dataArray: JSON.stringify(this.dataArray)
				}).then((res) => {
					if (res.code == 0) {
						uni.redirectTo({
							url: "/pages/HomePage/WarehouseManage/StorageManagement/Inventory/index"
						})
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			}
		}
	}
</script>
<style lang="scss" scoped>
</style>